﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace MetroManagement.EntityFrameworkCore
{
    /// <summary>
    /// 地铁管理数据上下文模型创建扩展类
    /// </summary>
    public static class MetroManagementDbContextModelCreatingExtensions
    {
        public static void ConfigureMetroManagement(
            this ModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            modelBuilder.Entity<Card>(b =>
            {
                b.ToTable("Cards");

                b.ConfigureByConvention();

                b.Property(x => x.UserName).IsRequired();
                b.Property(x => x.IdCard).IsRequired();
                b.Property(x => x.Phonenumber).IsRequired();
                b.Property(x => x.CardNum).IsRequired();

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Notice>(b =>
            {
                b.ToTable("Notices");

                b.ConfigureByConvention();

                b.Property(x => x.Title).IsRequired();
                b.Property(x => x.Content).IsRequired();
            });

            modelBuilder.Entity<Statement>(b =>
            {
                b.ToTable("Statements");

                b.ConfigureByConvention();

                b.Property(x => x.Title).IsRequired();
                b.Property(x => x.Content).IsRequired();
            });

            modelBuilder.Entity<Found>(b =>
            {
                b.ToTable("Founds");

                b.ConfigureByConvention();

                b.Property(x => x.ImgUrl).IsRequired();
                b.Property(x => x.FoundPlace).IsRequired();
                b.Property(x => x.ClaimPlace).IsRequired();
                b.Property(x => x.PublishDate).HasDefaultValueSql("GETDATE()");
            });

            modelBuilder.Entity<City>(b =>
            {
                b.ToTable("Cities");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.ImgUrl).IsRequired();

                // 城市、站点一对多
                b.HasMany(f => f.Stations).WithOne(f => f.City);
                // 城市、线路一对多
                b.HasMany(f => f.Lines).WithOne(f => f.City);
            });

            modelBuilder.Entity<Station>(b =>
            {
                b.ToTable("Stations");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();

                b.HasMany(f => f.LineStations).WithOne(f => f.Station).OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<Line>(b =>
            {
                b.ToTable("Lines");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.StartTime).IsRequired();
                b.Property(x => x.EndTime).IsRequired();

                b.HasMany(f => f.LineStations).WithOne(f => f.Line).OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<LineStation>(b =>
            {
                b.ToTable("LineStations");

                b.ConfigureByConvention();

                b.HasKey(m => new { m.LineId, m.StationId });

                b.HasIndex(m => m.LineId).IsUnique(false);
                b.HasIndex(m => m.StationId).IsUnique(false);
            });

            modelBuilder.Entity<Record>(b =>
            {
                b.ToTable("Records");

                b.ConfigureByConvention();

                b.Property(x => x.Start).IsRequired();
                b.Property(x => x.Stop).IsRequired();
                b.Property(x => x.CreateTime).HasDefaultValueSql("GETDATE()");

                b.HasIndex(m => m.LineId).IsUnique(false);

                b.HasOne(f => f.Line);
            });
        }
    }
}